// login.js
var app = getApp()
var lastClickTime = 0
var minClickTime = app.globalData.minClickTime
var encryptUtils = require('../../utils/HHEncryptUtils.js')
var modelUtils = require('../../utils/HHModelUtils.js')
var md5 = require('../../utils/md5.js')
var baseImgPath = app.globalData.imgBasepath

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userHead: baseImgPath + app.globalData.login_head,
        del: "../../images/r_del.png",
        login_tel: "../../images/login_phone.png",
        login_psw: "../../images/login_psw.png"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {


    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    formSubmit: function (event) {
        var loginname = event.detail.value.phone
        var psw = event.detail.value.password

        if (loginname.length == 0) {
            wx.showToast({
                title: '请输入手机号',
            })
            return;
        }
        if (psw.length == 0) {
            wx.showToast({
                title: '请输入密码',
            })
            return;
        }
        this.toLogin(loginname, psw)
    },
    toLogin: function (logname, psw) {
        if (wx.showLoading) {
            wx.showLoading({
                title: '正在登录',
            })
        }
        var para = {}
        para["login_name"] = encryptUtils.base64Encode(logname);
        para["login_pwd"] = encryptUtils.base64Encode(md5.hex_md5(md5.hex_md5(psw)));
        var str = JSON.stringify(para)
        var urlStr = encodeURIComponent(str)
        console.log("urlStr==" + urlStr)
        wx.request({
            url: app.globalData.ip + "userlogin",
            method: 'POST',
            header:
            {
                'From': "WeChat"
            },
            data:
            {
                'para': urlStr
            },
            success: function (res) {
                var code = res.data.code
                console.log("res==" + JSON.stringify(res))
                if (code == 100) {
                    var userInfo = modelUtils.getModel(res.data.result)
                    console.log("user_id==" + userInfo.user_id)
                    wx.setStorageSync("user_id", userInfo.user_id)
                    wx.setStorageSync("user_type", userInfo.role_type)

                } else {
                    var message = res.data.msg
                    wx.showToast({
                        title: message,
                    })
                }

            },
            fail: function () {
                wx.showToast({
                    title: '网络异常',
                })
            }

        })
    }
  


})